package Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static Utils.MyLogger.logInfo;
/**
 * @author 锄禾
 * @projectName MkPack
 * @description: 获取移动设备相关信息
 */
public class GetDevices {

    /**
    　* @description: 获取Android设备sdk版本号
    　* @param
    　* @return
    　*/
    public static int getSDKVersion(String udid) throws IOException {
        int sdkversion = -1;
        String cmd = "adb -s" + udid + " shell getprop | findstr version.sdk";
        Process p = Runtime.getRuntime().exec(cmd);
        InputStream is = p.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null){
            int length = line.length();
            int index = line.indexOf(":");
            line = line.substring(index + 1, length).trim().substring(1,3);
            logInfo("SDK版本号为：" + line);
            sdkversion = Integer.valueOf(line);
        }
        is.close();
        reader.close();
        p.destroy();
        return sdkversion;
    }

    /**
    　* @description: 获取Android设备型号
    　* @param
    　* @return
    　*/
    public static String getDevicesModel(String udid) throws IOException {
        String model = "google";
        try {
            String cmd = "adb -s " + udid + " shell getprop ro.product.model";
            Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null){
                model = line;
                break;
            }
            is.close();
            reader.close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return model;
        }
    }

    /**
    　* @description: 获取Android设备版本号
    　* @param udid
    　* @return
    　*/
    public static String getDevicesVersion(String udid) throws IOException {
        String version = "6.0";
        try {
            String cmd = "adb -s " + udid + " shell getprop ro.build.version.release";
            Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line ;
            while ((line = reader.readLine()) != null) {
                version = line;
                break;
            }
            is.close();
            reader.close();
            p.destroy();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return version;
        }
    }

    /**
    　* @description: 获取app版本号
    　* @param
    　* @return
    　*/
    public static String getAppVersion(String udid, String apkname){
        String version = "";
        try {
            String cmd = "adb -s " + udid + " shell am dump " + apkname + " | findstr 'versionName'";
            Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null){
                version = line.split("=")[1];
                break;
            }
            is.close();
            reader.close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return version;
        }
    }

    /**
    　* @description: 获取apk包大小
    　* @param
    　* @return
    　*/
    public static String getApkSize(String apkPath){
        String apkSize = "0M";
        try {
            String cmd = "du -sh " + apkPath;
            Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line ;
            while ((line = reader.readLine()) != null) {
                apkPath = line.split("\\s+")[1];
                break;
            }
            is.close();
            reader.close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return apkPath;
        }
    }
}
